import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

export function request(config) {

    //1.创建axios实例
    let instance = axios.create({
        //根路径
        baseURL: 'http://xq.svger.cn/',
        // baseURL:"/api",
        timeout: 10000,
        withCredentials: true,
    })


    // 2.axios的拦截器
    // 2.1.请求拦截的作用
    instance.interceptors.request.use(config => {
        const cookie = sessionStorage.getItem('cookie')
        config.params.cookie = cookie;
        // const token = sessionStorage.getItem("token")
        // if(token){
        //   config.headers.Authorization = 'Bearer'+token
        // }
        NProgress.start()
        return config
    }, err => {
        NProgress.done()

        return err
    })

    // 2.2.响应拦截
    instance.interceptors.response.use(res => {
        NProgress.done()
        return res
    }, err => {
        NProgress.done()
        return err
    })

    // 3.发送真正的网络请求
    return instance(config)

}